<style lang="scss" scoped>
	.content {
		width: 100%;
		background-color: white;

		padding: 30rpx 25rpx;
	}
</style>

<template>
	<view class="content">
		<view class="f34 bold" style="padding:0 30rpx;border-bottom: 1px solid #eee;padding-bottom: 20rpx;">转工单</view>
		<evan-form :model="formData" ref="formData">

			<evan-form-item label="报修人" prop="repairPeople">
				<u-input v-model="formData.repairPeople" placeholder="输入报修人" input-align="right" />
			</evan-form-item>
			<evan-form-item label="报修人电话" prop="repairPeoplePhone">
				<u-input v-model="formData.repairPeoplePhone" placeholder="输入报修人电话" input-align="right" />
			</evan-form-item>
			<evan-form-item label="报修科室" prop="department">
				<ItemRow ref="department" v-model="formData.department" chooseType="select-department"></ItemRow>
			</evan-form-item>
			<evan-form-item label="报修时间" prop="time">
				<ItemRow ref="time" v-model="formData.time" chooseType="date-picker-ymd"></ItemRow>

			</evan-form-item>
			<evan-form-item label="紧急程度" required>
				<ItemRow v-model="formData.degree" dic-key="gradeUrgency"></ItemRow>
			</evan-form-item>

			<evan-form-item label="故障描述" prop="faultMemo">
				<u-input v-model="formData.faultMemo" type="textarea" placeholder="输入故障描述" input-align="right" />

			</evan-form-item>

			<evan-form-item label="故障分类" prop="faultType">
				<ItemRow v-model="formData.faultType" dic-key="faultClassification" chooseType="select-dic-cascader"></ItemRow>
			</evan-form-item>
			<evan-form-item label="影响范围" required>
				<ItemRow v-model="formData.rangeType" dic-key="faultScope"></ItemRow>
			</evan-form-item>
			<evan-form-item label="指派人员" prop="assignedPeople">
				<UserSelect v-model="formData.assignedPeople" />
				<ItemRow v-model="formData.assignedPeople" chooseType="select-user"></ItemRow>
			</evan-form-item>
			<evan-form-item label="预计工时" prop="expectedTime">
				<view class="u-flex">
					<view class="u-flex-4">
						<u-input v-model="formData.expectedTime" placeholder="输入预计工时" input-align="right" />
					</view>
					<view class="u-flex-1">
						小时
					</view>
				</view>

			</evan-form-item>
		</evan-form>





		<view class="lbtn" @click="submit">提交</view>
	</view>
</template>

<script>
	import PagesCourseRouterConfig from '@/pagesCourse/routerConfig.js'
	import ItemRow from '@/components/arrow/itemArrow.vue'
	import ProjectApi from '@/apis/project.js'
	export default {

		components: {
			ItemRow
		},
		props: {
			itemCode: {
				default: ''
			},
			data: {
				default: {}
			},
		},
		data() {
			return {
				PagesCourseRouterConfig,
				formData: {
					// 报修人
					repairPeople: '',
					// 报修人电话
					repairPeoplePhone: '',
					// 故障分类
					faultType: '',
					// 故障描述
					faultMemo: '',
					// 报修时间
					time: '',
					// 指派人
					assignedPeople: '',
					// 报修科室
					department: null,
					// 预计工时
					expectedTime: '',
					// 项目优先级
					degree: 3,
					// 影响范围
					rangeType: 1,
				},
				rules: {
					repairPeople: [{
						required: true,
						message: '报修人不能为空',
					}],
					repairPeoplePhone: [{
						required: true,
						message: '电话不能为空',
					}],
					faultType: [{
						required: true,
						message: '故障分类',
					}],
					department: [{
						required: true,
						message: '请选择报修科室',
					}],
					faultMemo: [{
						required: true,
						message: '请填写故障描述',
					}],
					time: [{
						required: true,
						message: '请选择报修时间',
					}],
					assignedPeople: [{
						required: true,
						message: '请选择指派人员',
					}],
					expectedTime: [{
						required: true,
						type: 'number',
						message: '工时只能为数字',
						transform: (v) => Number(v),

					}, {
						validator: (r, v, cb) => {
							if (parseFloat(v) <= 0 || v === '') {
								return cb(new Error('工时不能为空或者小于0'));
							}
							cb();
						},
					}],
				},
			}
		},
		mounted() {
			this.init()
			this.$nextTick(() => {
				this.$refs.formData.setRules(this.rules)
			})
		},
		methods: {
			init() {
				this.formData.repairPeople = this.data.proposerList[0].sponsor;
				this.formData.repairPeoplePhone = this.data.proposerList[0].phone;
				this.formData.department = this.data.proposerList[0].organizer;

				this.formData.time = this.data.startTime;
				this.formData.faultMemo = this.data.title + this.data.remark || '';
				this.$refs.time.resetInit(this.$options.filters.date(this.formData.time))
				this.$refs.department.resetInit(this.$options.filters.subject(this.formData.department))

			},
			submit() {


				this.$refs['formData'].validate(async (valid) => {
					if (valid) {
						const submitArguments = {
							itemCode: this.itemCode,
							user: this.formData.repairPeople,
							phone: this.formData.repairPeoplePhone,
							createTime: this.formData.time,
							deptId: this.formData.department,
							faultRemark: this.formData.faultMemo,
							sendUser: this.formData.assignedPeople,
							reqTime: this.formData.expectedTime,
							faultType: this.formData.faultType,
							degree: this.formData.degree,
							learningId: this.formData.learningId,
							rangeType: this.formData.rangeType,
						};
						await ProjectApi.Demand.ToOrder(submitArguments);
						this.$msg('提交成功！')
						setTimeout(() => {
							wx.navigateBack()
						}, 1500)

					}
				});


			},




		}
	}
</script>
